$(function() {
		var cache = {},
			lastXhr;
		$( "#search" ).autocomplete({
			minLength: 3,
                        delay: 1000,
                        select: function( event, ui ) {
				window.location.href= "user/name/" + ui.item.value;
			},
			source: function( request, response ) {
				var term = request.term;
				if ( term in cache ) {
					response( cache[ term ] );
					return;
				}

				lastXhr = $.getJSON( "users/search/" +  $(':radio[name="search"]:checked').attr("value") + "/" + term, null, function( data, status, xhr ) {
					cache[ term ] = data;
					if ( xhr === lastXhr ) {
						response( data );
					}
                                       
				});//alert();
			}
	
            });
	});